package com.getpebble.android.notifications.model;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.app.PendingIntent.OnFinished;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.RemoteInput;
import android.text.TextUtils;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblPreferences;
import com.getpebble.android.common.model.PblNotificationModel.Record;
import com.getpebble.android.framework.notification.PblNotificationProcessor.InvokeActionResult;
import com.getpebble.android.framework.notification.gmail.GmailCheckerService;
import com.getpebble.android.framework.notification.gmail.GmailCheckerService.ActionCallback;
import com.getpebble.android.framework.notification.gmail.GmailCheckerService.GmailAction;
import com.getpebble.android.framework.notification.gmail.GmailCheckerService.MessageId;
import com.getpebble.android.framework.receiver.SMSReceiver;
import com.getpebble.android.notifications.PblNotificationService;
import com.sun.mail.smtp.SMTPMessage;
import java.util.ArrayList;
import java.util.List;

public class Actions {
    private static final String TAG = Actions.class.getSimpleName();

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$notification$gmail$GmailCheckerService$GmailAction = new int[GmailAction.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$notification$gmail$GmailCheckerService$GmailAction[GmailAction.MARK_AS_READ.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$notification$gmail$GmailCheckerService$GmailAction[GmailAction.STAR_AND_MARK_READ.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
        }
    }

    public static class NotificationDismissKey {
        public final int mId;
        public final String mKey;
        public final String mPackageName;
        public final String mTag;

        private NotificationDismissKey(int id, String tag, String packageName, String key) {
            this.mId = id;
            this.mTag = tag;
            this.mPackageName = packageName;
            this.mKey = key;
        }

        public static NotificationDismissKey from(PblNotification notification) {
            return new NotificationDismissKey(notification.getAndroidNotificationId(), notification.getAndroidNotificationTag(), notification.getPackageName(), notification.getAndroidNotificationKey());
        }

        public static NotificationDismissKey from(Record record) {
            return new NotificationDismissKey(record.androidNotificationId, record.androidNotificationTag, record.androidPackageName, record.androidNotificationKey);
        }
    }

    public static abstract class PblNotificationAction {
        private int mId;
        private CharSequence mTitle;

        private PblNotificationAction(CharSequence title, int id) {
            this.mId = id;
            this.mTitle = title;
        }

        public int getId() {
            return this.mId;
        }

        public CharSequence getTitle() {
            return this.mTitle;
        }

        public void setId(int id) {
            this.mId = id;
        }
    }

    public static class PblNotificationAndroidAction extends PblNotificationAction {
        private PendingIntent mIntent;

        public PblNotificationAndroidAction(CharSequence title, PendingIntent intent, int id) {
            super(title, id);
            this.mIntent = intent;
            Trace.debug(Actions.TAG, "Adding " + getClass().getSimpleName() + " id = " + getId() + " title = " + title);
        }

        public boolean sendAction() {
            if (this.mIntent == null) {
                Trace.warning(Actions.TAG, "Intent is null");
                return false;
            }
            try {
                this.mIntent.send(0, new OnFinished() {
                    public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, String resultData, Bundle resultExtras) {
                        Trace.verbose(Actions.TAG, "PblNotificationAndroidAction onSendFinished pendingIntent = " + pendingIntent + " intent = " + intent + " resultCode = " + resultCode + " resultData = " + resultData + " resultExtras = " + resultExtras);
                    }
                }, null);
                return true;
            } catch (CanceledException e) {
                Trace.warning(Actions.TAG, "Error sending notification action", e);
                return false;
            }
        }
    }

    public static class PblNotificationDismissAction extends PblNotificationAction {
        NotificationDismissKey mDismissKey;

        public PblNotificationDismissAction(CharSequence title, int id, NotificationDismissKey dismissKey) {
            super(title, id);
            this.mDismissKey = dismissKey;
            Trace.debug(Actions.TAG, "Adding PblNotificationDismissAction id = " + getId());
        }

        public void dismiss() {
            PblNotificationService.dismissNotification(this.mDismissKey);
        }
    }

    public static class PblNotificationGmailAction extends PblNotificationAction {
        private final String mAccount;
        private final MessageId mMessageId;
        private final GmailAction mType;

        private static String getTitle(GmailAction type) {
            switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$notification$gmail$GmailCheckerService$GmailAction[type.ordinal()]) {
                case SMTPMessage.RETURN_FULL /*1*/:
                    return PebbleApplication.getAppContext().getString(R.string.mark_as_read_action_title);
                case SMTPMessage.RETURN_HDRS /*2*/:
                    return PebbleApplication.getAppContext().getString(R.string.star_action_title);
                default:
                    return null;
            }
        }

        public PblNotificationGmailAction(PblNotification notification, GmailAction type, String account, int id) throws IllegalArgumentException {
            super(getTitle(type), id);
            this.mType = type;
            this.mMessageId = MessageId.from(notification);
            this.mAccount = account;
        }

        public void invoke(ActionCallback callback) {
            GmailCheckerService.performMessageAction(this.mAccount, this.mType, this.mMessageId, callback);
        }
    }

    public static class PblNotificationOpenOnPhoneAction extends PblNotificationAndroidAction {
        public PblNotificationOpenOnPhoneAction(CharSequence title, PendingIntent intent, int id) {
            super(title, intent, id);
        }
    }

    public static class PblNotificationReplyAction extends PblNotificationAction {
        private final List<CharSequence> mChoices;
        private final PendingIntent mIntent;
        private final RemoteInput mRemoteInput;

        public PblNotificationReplyAction(RemoteInput input, CharSequence title, PendingIntent intent, int id, List<CharSequence> choices) throws IllegalArgumentException {
            super(title, id);
            this.mRemoteInput = input;
            this.mIntent = intent;
            this.mChoices = choices;
        }

        public void sendReply(String reply, Context context, ActionCallback callback) {
            Trace.verbose(Actions.TAG, "PblNotificationReplyAction: sendReply");
            if (this.mIntent == null) {
                Trace.debug(Actions.TAG, "Intent is null, cannot send reply");
                callback.result(InvokeActionResult.FAILED);
            } else if (context == null) {
                Trace.debug(Actions.TAG, "context is null, cannot send reply");
                callback.result(InvokeActionResult.FAILED);
            } else {
                Intent fillInIntent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString(this.mRemoteInput.getResultKey(), reply);
                RemoteInput.addResultsToIntent(new RemoteInput[]{this.mRemoteInput}, fillInIntent, bundle);
                try {
                    this.mIntent.send(context, 0, fillInIntent, new OnFinished() {
                        public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, String resultData, Bundle resultExtras) {
                            Trace.verbose(Actions.TAG, "PblNotificationReplyAction onSendFinished pendingIntent = " + pendingIntent + " intent = " + intent + " resultCode = " + resultCode + " resultData = " + resultData + " resultExtras = " + resultExtras);
                        }
                    }, null);
                    callback.result(InvokeActionResult.REPLIED);
                } catch (CanceledException e) {
                    Trace.warning(Actions.TAG, "Error sending notification action", e);
                    callback.result(InvokeActionResult.FAILED);
                }
            }
        }

        public List<CharSequence> getChoices() {
            return this.mChoices;
        }
    }

    public static class PblNotificationSMSReplyAction extends PblNotificationReplyAction {
        private final String mPhoneNumber;

        private static List<CharSequence> getDefaultChoices() {
            ArrayList<CharSequence> choices = new ArrayList();
            SharedPreferences sp = PblPreferences.getSharedPreferences(PebbleApplication.getAppContext());
            for (int i = 0; i < 5; i++) {
                String response = sp.getString("canned_response_" + i, null);
                if (!TextUtils.isEmpty(response)) {
                    choices.add(response);
                }
            }
            return choices;
        }

        public PblNotificationSMSReplyAction(String phoneNumber, int id) {
            super(null, PebbleApplication.getAppContext().getString(R.string.reply_action_title), null, id, getDefaultChoices());
            this.mPhoneNumber = phoneNumber;
        }

        public void sendReply(String reply, Context context, ActionCallback callback) {
            Trace.verbose(Actions.TAG, "PblNotificationSMSReplyAction: sendReply");
            SMSReceiver.sendSmsReply(reply, this.mPhoneNumber, callback);
        }
    }
}
